package com.jf.anmocker.plugin.mockcore.asm

import org.objectweb.asm.AnnotationVisitor
import org.objectweb.asm.Type
import org.objectweb.asm.tree.MethodNode

class AnMockerMethodNode(
    api: Int,
    access: Int,
    name: String?,
    descriptor: String?,
    signature: String?,
    exceptions: Array<out String>?
) : MethodNode(api, access, name, descriptor, signature, exceptions) {

    private var isMethodMocker = false
    private var methodMockInfo: MethodAnoEntity? = null
    private var mockerInfo : AnMockerClzNode? = null

    override fun visitAnnotation(descriptor: String?, visible: Boolean): AnnotationVisitor {
        val annotation = MethodAnoNode(descriptor, api).apply {
            setMethodVisitListener(object : MethodAnoVisitListener {
                override fun methodVisited(
                    isMethodMocker: Boolean,
                    methodMockInfo: MethodAnoEntity?
                ) {
                    this@AnMockerMethodNode.isMethodMocker = isMethodMocker
                    this@AnMockerMethodNode.methodMockInfo = methodMockInfo
                }
            })
        }
        if (visible) {
            visibleAnnotations = visibleAnnotations ?: ArrayList()
            visibleAnnotations.add(annotation)
        } else {
            invisibleAnnotations = invisibleAnnotations ?: ArrayList()
            invisibleAnnotations.add(annotation)
        }
        return annotation
    }

    override fun visitEnd() {
        AsmMockManager.getInstance().checkForMethodMock(this, mockerInfo)
    }

    public fun isMethodMocker(): Boolean {
        return isMethodMocker
    }

    public fun getMethodMockInfo(): MethodAnoEntity? {
        return methodMockInfo
    }

    public fun injectMockerInfo(classMocker : AnMockerClzNode?){
        mockerInfo = classMocker
        //如果是mock目标则处理mock信息
        //AsmMockManager.getInstance().checkForMethodMock(methodAno, classMocker)
    }
}